using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class enemy : MonoBehaviour
{
    private List<Vector3> landmarks = new List<Vector3>();
    //landmarks是导航点的列表,敌人会依次移动到每个导航点
    public int speed = 10;
    //speed表示敌人移动的速度
    public int hp_max = 100;
    public float hp = 100;
    public int itegral;
    private int lastHp;
    public GameObject lifeBar;
    public Slider slider;
    private Material material_origin;
    public GameObject numnber;
    private Text numbers;
    private Text CountNumber;
    private int num;
    //获取manager的游戏对象
    private GameObject manage;
    //当前游戏关卡数
    private int level;
    private TMPro.TextMeshProUGUI textMeshProUGUI;
    public int ReceiveDamage = 20;
    // Start is called before the first frame update  
    private tank tanks;
    void Start()
    {
        //获取子弹伤害
        tanks = GameObject.FindGameObjectWithTag("tank").GetComponent<tank>();
        
        levelDesign();
        //Start方法在敌人生成后直接调用
        lastHp = (int)hp;
        material_origin = new Material(gameObject.transform.GetComponent<Renderer>().material);
        //使用动态数组list来依次获取所有的路标的坐标       
        foreach (Transform element in GameObject.Find("landmarks").transform)
        {
            landmarks.Add(element.position);
        }
        //实例化资源文件夹下的LifeBar预制体,赋值给canvas_now变量 
        lifeBar = Instantiate((GameObject)Resources.Load("LifeBar"));
        //获取血条的silder
        slider = lifeBar.transform.GetChild(0).gameObject.GetComponent<Slider>();
        numnber = GameObject.Find("intergral");

    }

    // Update is called once per frame

    void Update()
    {
        //更新子弹伤害
        ReceiveDamage = tanks.gameObject.GetComponent<tank>().harm;
        if (numnber != null)
        {
            numbers = numnber.transform.Find("number").GetComponent<Text>();
            CountNumber = numnber.transform.Find("countNumber").GetComponent<Text>();
            num = int.Parse(numbers.text);
        }

        if (hp != lastHp)
        {
            lastHp = (int)hp;
            slider.value = (float)hp / hp_max;
            if (hp <= 0)
            {

                //如果hp小于等于0,敌人被消灭
                //执行以下语句
                num = num + itegral;
                numbers.text = num.ToString();
                CountNumber.text = (int.Parse(CountNumber.text)+itegral).ToString();
                GameObject exp = Instantiate((GameObject)Resources.Load("explode"));
                //从Resources文件夹加载名为effect的预制体,并实例化

                exp.transform.position = gameObject.transform.position;
                //将实例化的游戏对象的位置设置为敌人的位置

                ParticleSystem effect = exp.GetComponent<ParticleSystem>();
                //获取实例化游戏对象上的ParticleSystem组件

                effect.Play();
                //播放粒子特效

                Destroy(gameObject);
                //销毁敌人游戏对象

                Destroy(exp, 1f);
                //1秒后销毁effect游戏对象

                Destroy(lifeBar);
                //销毁与敌人绑定的canvas_now游戏对象

            }
        }
        //Update方法在每个帧重复调用
        if (landmarks.Count > 0)
        {
            //如果导航点列表不为空,获取第一个点的方向和位置
            Vector3 l_direction = new Vector3(landmarks[0].x, transform.position.y, landmarks[0].z);

            //使敌人朝向下一个导航点的方向
            transform.LookAt(l_direction);

            //移动到导航点
            transform.Translate(Vector3.forward * Time.deltaTime * speed, Space.Self);
            //如果敌人离第一个导航点很近(小于1)
            if (Vector3.Distance(gameObject.transform.position, landmarks[0]) < 1f)
            {
                //从landmarks列表中移除第一个导航点
                landmarks.RemoveAt(0);
            }
        }
        lifeBar.transform.position = gameObject.transform.position;
        //将canvas_now的位置设置为敌人的位置
        //然后y轴上移
        lifeBar.transform.position += new Vector3(0, 1.5f, 0);
    }
    private void OnTriggerEnter(Collider other)
    {
        //当其他碰撞器进入敌人的碰撞器碰撞区时调用此方法

        //判断进入碰撞区的游戏对象的标签是否为ball
        if (other.gameObject.tag == "ball")
        {
            //如果为ball,执行以下语句
            //添加以下代码
            gameObject.GetComponent<Renderer>().material.color = Color.red;
            //延迟0.5秒
            Invoke("recoverColor", 0.2f);
            hp -= ReceiveDamage;
            //将hp减20

            slider.value = (float)hp / hp_max;
            //根据当前hp和最大hphp_max计算slider的value值,更新slider显示

            if (hp < 0)
            {
                //如果hp小于等于0,敌人被消灭
                //执行以下语句
                num = num + itegral;
                numbers.text = num.ToString();
                CountNumber.text = (int.Parse(CountNumber.text)+itegral).ToString();
                GameObject exp = Instantiate((GameObject)Resources.Load("explode"));
                //从Resources文件夹加载名为effect的预制体,并实例化

                exp.transform.position = gameObject.transform.position;
                //将实例化的游戏对象的位置设置为敌人的位置

                ParticleSystem effect = exp.GetComponent<ParticleSystem>();
                //获取实例化游戏对象上的ParticleSystem组件

                effect.Play();
                //播放粒子特效

                Destroy(gameObject);
                //销毁敌人游戏对象

                Destroy(exp, 1f);
                //1秒后销毁effect游戏对象

                Destroy(lifeBar);
                //销毁与敌人绑定的canvas_now游戏对象
            }
        }
    }
    void recoverColor()
    {
        //使用默认材质恢复默认颜色
        gameObject.GetComponent<Renderer>().material = material_origin;
    }
    //关卡设计
    void levelDesign()
    {
        //manager的游戏对象获取
        //查找manager游戏对象
        GameObject managerObj = GameObject.Find("manager");
        //获取manager组件
        manager manager = managerObj.GetComponent<manager>();
        //访问level参数
        level = manager.level;
        hp_max = hp_max + ((level-1)*100);
        hp = hp + ((level-1)*100);
        Debug.Log("当前关卡数:" + level);
    }
}
